package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.Klass;
import com.mindskip.xzs.domain.KlassUser;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.KlassService;
import com.mindskip.xzs.service.KlassUserService;
import com.mindskip.xzs.service.UserService;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.klass.KlassEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.klass.KlassPageRequestVM;
import com.mindskip.xzs.viewmodel.admin.klass.KlassResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController("KlassController")
@RequestMapping(value = "/api/teacher/klass")
public class KlassController extends BaseApiController {

    private final KlassService klassService;
    @Autowired
    private UserService userService;

    @Autowired
    private KlassUserService klassUserService;

    public KlassController(KlassService klassService) {
        this.klassService = klassService;
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
        public RestResponse<List<Klass>> list (){
        List<Klass> klass = klassService.allKlass();
        return RestResponse.ok(klass);
    }

    @RequestMapping(value = "/page",method = RequestMethod.POST)
    public RestResponse<PageInfo<KlassResponseVM>> pageList(@RequestBody KlassPageRequestVM model){
        User currentUser = getCurrentUser();
        if (currentUser.getRole()==2){
            model.setTeacherId(String.valueOf(currentUser.getId()));
        }
        PageInfo<Klass> pageInfo = klassService.page(model);
        PageInfo<KlassResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> modelMapper.map(e, KlassResponseVM.class));
        return RestResponse.ok(page);
    }
    @RequestMapping(value = "bindTeacher",method = RequestMethod.POST)
    public RestResponse bindTeacher(@RequestParam String teacherName,@RequestParam Integer klassId){
        User user = userService.selectByName(teacherName);
        if (user == null){
            return RestResponse.fail(500,"用户不存在");
        }else {
            KlassUser klassUser = new KlassUser();
            klassUser.setUserId(user.getId());
            klassUser.setKlassId(klassId);
            klassUserService.insert(klassUser);
            Klass klass = klassService.selectById(klassId);
            klass.setNum(klass.getNum()+1);
            klassService.updateById(klass);
            return RestResponse.ok();
        }

    }


    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid KlassEditRequestVM model){
        Klass klass = modelMapper.map(model, Klass.class);
        if (model.getId() == null){
            klass.setDeleted(false);
            // 设置 countersign 字段为 UUID
            klass.setCountersign(UUID.randomUUID().toString());
            // 设置创建时间为当前时间
            klass.setCreateTime(new Date());
            klass.setNum(0);
            klass.setDeleted(false);
            klassService.insertByFilter(klass);
        } else {
            klassService.updateById(klass);
        }
        return RestResponse.ok();
    }

    @RequestMapping(value = "/select/{id}",method = RequestMethod.POST)
    public RestResponse select(@PathVariable Integer id){
        Klass klass = klassService.selectById(id);
        KlassEditRequestVM map = modelMapper.map(klass, KlassEditRequestVM.class);
        return RestResponse.ok(map);
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id){
        Klass klass = klassService.selectById(id);
        if (klass == null) {
            return RestResponse.fail(1,"指定的班级不存在");
        }
        klass.setDeleted(true);
        klassService.updateById(klass);
        return RestResponse.ok();
    }


}
